<template>
  <div class="search-div">
    <!-- 搜索表单 -->
    <el-form label-width="70px" size="small">
      <el-form-item label="角色名称">
        <el-input
          style="width: 100%"
          placeholder="角色名称"
          v-model="queryDto.roleName"
        ></el-input>
      </el-form-item>
      <el-row style="display: flex">
        <el-button type="primary" size="small" @click="searchSysRole">
          搜索
        </el-button>
        <el-button size="small" @click="resetData">重置</el-button>
      </el-row>
    </el-form>

    <!-- 添加按钮 -->
    <div class="tools-div">
      <el-button type="success" size="small" @click="addShow">添 加</el-button>
    </div>

    <!-- 添加表单 -->
    <!-- 添加角色表单对话框 -->
    <el-dialog v-model="dialogVisible" title="添加或修改角色" width="30%">
      <el-form label-width="120px">
        <el-form-item label="角色名称">
          <el-input v-model="sysRole.roleName" />
        </el-form-item>
        <el-form-item label="角色Code">
          <el-input v-model="sysRole.roleCode" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="sysRole.description" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--- 角色表格数据 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="roleName" label="角色名称" width="180" />
      <el-table-column prop="roleCode" label="角色code" width="180" />
      <el-table-column prop="createTime" label="创建时间" />

      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button type="primary" size="small" @click="editShow(scope.row)">
          修改
        </el-button>
        <el-button type="danger" size="small" @click="deletById(scope.row.id)">
          删除
        </el-button>
        <el-button
          type="warning"
          size="small"
          @click="showAssignMenu(scope.row)"
        >
          分配菜单
        </el-button>
      </el-table-column>
    </el-table>

    <!-- 分配菜单的对话框 
            // tree组件添加ref属性，后期方便进行tree组件对象的获取
            -->
    <el-dialog v-model="dialogMenuVisible" title="分配菜单" width="40%">
      <el-form label-width="80px">
        <el-tree
          :data="sysMenuTreeList"
          ref="tree"
          show-checkbox
          default-expand-all
          :check-on-click-node="true"
          node-key="id"
          :props="defaultProps"
        />
        <el-form-item>
          <el-button type="primary" @click="doAssign">提交</el-button>
          <el-button @click="dialogMenuVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--分页条-->
    <el-pagination
      v-model:current-page="pageParem.page"
      v-model:page-size="pageParem.limit"
      :page-sizes="[5, 10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {
  GetSysRoleListByPage,
  SaveSysRole,
  UpdateSysRole,
  DeleteSysRoleById,
  GetSysRoleMenuIds,
  DoAssignMenuIdToSysRole
} from '@/api/sysRole'
import { ElMessage, ElMessageBox } from 'element-plus'
//分配菜单----------------------------
//定义分配角色保存的事件方法
const doAssign = async () => {
  //获取选中的节点
  const checkedNodes = tree.value.getCheckedNodes()
  //遍历过滤需要的数据 [{id:1},{id:2}]
  const checkedNodesIds = checkedNodes.map(node => {
    return {
      menuId: node.id,
      isHalf: 0,
    }
  })

  //获取选中的半节点

  const halfCheckedNodes = tree.value.getHalfCheckedNodes()
  //遍历过滤需要的数据 [{id:1},{id:2}]
  const halfCheckedNodesIds = halfCheckedNodes.map(node => {
    return {
      menuId: node.id,
      isHalf: 1,
    }
  })
  //合并选中和半选中节点
  const menuIdList = [...checkedNodesIds, ...halfCheckedNodesIds]
  console.log(menuIdList)

  //整合数据
  let queryDto = {
    roleId: roleId.value,
    menuIdList: menuIdList,
  }

  //请求后端保存
  const { code } = await DoAssignMenuIdToSysRole(queryDto)

  //判断
  if (code === 200) {
    //提示
    ElMessage.success('分配成功')
    //关闭对话框
    dialogMenuVisible.value = false
    //刷新
    fetchData()
  }
}

//定义变量接收角色id
const roleId = ref()

//分配角色回显---------------------
//定义分配角色对话框的变量
const dialogMenuVisible = ref(false)
//定义树形结构列表数据模型
const sysMenuTreeList = ref([])
//定义tree对象
const tree = ref()

//定义显示的true内容属性
const defaultProps = ref({
  children: 'children',
  label: 'title',
})

//定义回显菜单列表的事件方法
const showAssignMenu = async row => {
  //赋值角色id
  roleId.value = row.id

  //显示对话框
  dialogMenuVisible.value = true
  //查询需要回显的数据
  const { data } = await GetSysRoleMenuIds(row.id)

  //赋值树形列表
  sysMenuTreeList.value = data.sysMenusTree
  //回显选中的菜单id
  console.log(tree)
  tree.value.setCheckedKeys(data.menuIds)
}

//角色删除--------------------------
const deletById = roleId => {
  //确认提示框
  ElMessageBox.confirm('哥们,你确定要删除这个角色吗?', 'Warning', {
    confirmButtonText: '确定删除',
    cancelButtonText: '取消删除',
    type: 'warning',
  })
    .then(async () => {
      const { code, message, data } = await DeleteSysRoleById(roleId)
      //判断
      if (code === 200) {
        //提示
        ElMessage.success('删除成功')
        //刷新页面
        fetchData()
      }
    })

    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}

//角色修改--------------------------
//定义显示修改对话框的事件方法
const editShow = row => {
  console.log(row)
  //赋值回显数据
  sysRole.value = row
  //显示修改对话框
  dialogVisible.value = true
}

//修改提交在submit中整合往下看
//角色新增--------------------------

//定义角色数据模型
const defaultForm = {
  id: '',
  roleName: '',
  roleCode: '',
  description: '',
}
//设置为响应式
const sysRole = ref(defaultForm)

//保存事件方法
const submit = async () => {
  //判断数据模型中是否存在id，存在：修改 不存在：新增
  if (!sysRole.value.id) {
    //请求保存api
    const { code, message, data } = await SaveSysRole(sysRole.value)
    //判断
    if (code === 200) {
      //提示成功
      ElMessage.success('角色新增成功')
      //     ElMessage({
      //     message: '角色新增成功',
      //     type: 'success',
      // })
      //隐藏
      dialogVisible.value = false

      //刷新页面
      fetchData()
    }
  } else {
    //存在id，执行修改保存
    const { code, message, data } = await UpdateSysRole(sysRole.value)
    //判断
    if (code === 200) {
      //提示
      ElMessage.success('修改成功')
      //隐藏对话框
      dialogVisible.value = false
      //刷新列表
      fetchData()
    }
  }
}

//定义显示新增对话框的事件方法
const addShow = () => {
  dialogVisible.value = true
}
//定义控制对话框显示的变量
const dialogVisible = ref(false)

//分页查询--------------------------

//定义分页条数改变事件方法
const handleSizeChange = () => {
  fetchData()
}
//定义当前页改变事件方法
const handleCurrentChange = () => {
  fetchData()
}

//定义重置方法
const resetData = () => {
  //去除条件内容
  queryDto.value = {}
  //刷新页面
  fetchData()
}

//定义搜索事件方法
const searchSysRole = () => {
  fetchData()
}

//定义条件模型
const queryDto = ref({
  roleName: '',
})

//封装分页数据
const defaultPageForm = {
  page: 1, //当前页
  limit: 5, //每页条数
}
//设置为响应式
const pageParem = ref(defaultPageForm)
//抽取查询分页列表方法
const fetchData = async () => {
  //获取列表数据
  const { code, message, data } = await GetSysRoleListByPage(
    pageParem.value.page,
    pageParem.value.limit,
    queryDto.value
  )
  //判断
  if (code === 200) {
    total.value = data.total
    list.value = data.list
  }
}

//定义页面挂载后发起请求
onMounted(async () => {
  fetchData()
})

// 分页条总记录数
let total = ref(0)

// 定义表格数据模型
let list = ref([
  {
    id: 9,
    roleName: '系统管理员',
    roleCode: 'xtgly',
    createTime: '2023-07-31',
  },
  {
    id: 10,
    roleName: '商品管理员',
    roleCode: 'spgly',
    createTime: '2023-07-31',
  },
])
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}

.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>